package is.web.xdumper.client.table;

import is.bean.entity.department.Department;
import is.bean.entity.employee.Employee;
import is.bean.entity.region.Region;
import is.web.xdumper.client.XdumperHtml;
import is.xdumper.type.Dump;
import java.util.Iterator;

/**
 *
 * @author aspr0
 */
public class XdumperRewriteTable
{
    public static String generate(Dump itemsNew, Dump itemsOld)
    {
        String tableCanvas =
                "<h2>%s</h2>"
                + "<table>"
                + "<thead>%s</thead>"
                + "<tbody>%s</tbody>"
                + "</table>";
        String thead =
                "<tr>"
                + "<td>Id</td>"
                + "<td>New item</td>"
                + "<td>Will rewrite</td>"
                + "<td>Old item</td>"
                + "</tr>";
        String rowCanvas =
                "<tr>"
                + "<td>%2$s</td>"
                + "<td style=\"color: red\" id=\"%1$s%2$sn\">%3$s</td>"
                + "<td>"
                + "<input "
                + "type=\"checkbox\" "
                + "id=\"%1$s%2$s\" "
                + "name=\"%1$s%2$s\" "
                + "value=\"%2$s\" "
                + "onclick=\"xdrtSwapColors(\'%1$s%2$s\')\">"
                + "</td>"
                + "<td style=\"color: green\" id=\"%1$s%2$so\">%4$s</td>"
                + "</tr>";
        int totalMaxId = 0;

        String tableDepartments = "";
        if (itemsOld.getDepartmentTable().size() > 0)
        {
            String rows = "";
            Iterator<Department> departmentsOld = itemsOld.getDepartmentTable().iterator();

            while (departmentsOld.hasNext())
            {
                Department departmentOld = departmentsOld.next();
                int idOld = departmentOld.getId();

                Iterator<Department> departmentsNew = itemsNew.getDepartmentTable().iterator();
                while (departmentsNew.hasNext())
                {
                    Department departmentNew = departmentsNew.next();
                    if (departmentNew.getId().equals(idOld))
                    {
                        rows += String.format(rowCanvas, "d", idOld, departmentNew.getName(), departmentOld.getName());

                        if (idOld > totalMaxId)
                        {
                            totalMaxId = idOld;
                        }
                    }
                }
            }

            tableDepartments = String.format(tableCanvas, "Departments", thead, rows);
        }

        String tableEmployees = "";
        if (itemsOld.getEmployeeTable().size() > 0)
        {
            String rows = "";
            Iterator<Employee> employeesOld = itemsOld.getEmployeeTable().iterator();

            while (employeesOld.hasNext())
            {
                Employee employeeOld = employeesOld.next();
                int idOld = employeeOld.getId();

                Iterator<Employee> employeesNew = itemsNew.getEmployeeTable().iterator();
                while (employeesNew.hasNext())
                {
                    Employee employeeNew = employeesNew.next();
                    if (employeeNew.getId().equals(idOld))
                    {
                        rows += String.format(rowCanvas, "e", idOld, employeeNew.getName(), employeeOld.getName());

                        if (idOld > totalMaxId)
                        {
                            totalMaxId = idOld;
                        }
                    }
                }
            }

            tableEmployees = String.format(tableCanvas, "Employees", thead, rows);
        }

        String tableRegions = "";
        if (itemsOld.getRegionTable().size() > 0)
        {
            String rows = "";
            Iterator<Region> regionsOld = itemsOld.getRegionTable().iterator();

            while (regionsOld.hasNext())
            {
                Region regionOld = regionsOld.next();
                int idOld = regionOld.getId();

                Iterator<Region> regionsNew = itemsNew.getRegionTable().iterator();
                while (regionsNew.hasNext())
                {
                    Region regionNew = regionsNew.next();
                    if (regionNew.getId().equals(idOld))
                    {
                        rows += String.format(rowCanvas, "r", idOld, regionNew.getName(), regionOld.getName());

                        if (idOld > totalMaxId)
                        {
                            totalMaxId = idOld;
                        }
                    }
                }
            }

            tableRegions = String.format(tableCanvas, "Regions", thead, rows);
        }

        String inputHiddenMaxId = String.format(new XdumperHtml().INPUT_HIDDEN, "maxid", totalMaxId);

        return tableDepartments + tableEmployees + tableRegions + inputHiddenMaxId;
    }
}
